<?php


namespace App\Models;

use Illuminate\Database\Query\Builder as BaseQueryBuilder;
use Illuminate\Support\Arr;

class Builder extends BaseQueryBuilder {
    public function whereIn($column, $values, $boolean = 'and', $not = false) {
        dump($column, $values);

        $newValues = [];

//        if (count(explode(',', $values)) > 1) {
//            $values = explode(',', $values);
//        }
        foreach ($values as $key => $value) {
            if (is_array($value)) {
                break;
            } else {
//                dd($value,explode(',', $value));
                $values[$key] = explode(',', $value);
            }
        }
        $values = Arr::flatten($values);
//        dd($values);
//        dd();
//        foreach ()
//        dd($values);
//        $values = [
//            [1, 2, 3, 6],
//            [4, 5],
//            [7],
//            [8]
//        ];
//        $values = [1, 2, 3, 6];
//        $this->where(function ($query) use ($column, $values) {
//            $this->whereIn($column,$values[0]);
//        });
//        $this->whereIn($column,[1,2,3,6]);
//        return $this;
//        //Here we implement custom support for multi-column 'IN'
//        //A multi-column 'IN' is a series of OR/AND clauses
//        //TODO: Optimization
//        if (is_array($column)) {
//        $this->where(function (\Illuminate\Database\Query\Builder $query) use ($column, $values) {
//            foreach ($values as $value) {
////                    $query->orWhereIn($column,$value);
//                foreach ($value as $v) {
//                    $query->orWhere($column, $v);
//                }
//            }
//        });
////
//        return $this;
//        }

        return parent::whereIn($column, $values, $boolean, $not);
    }

    public function toSql() {
        $this->applyBeforeQueryCallbacks();
        if ($this->grammar->compileSelect($this) == 'select * from "teacher" where "teacher"."id" in (?, ?, ?, ?, ?, ?, ?, ?) and "teacher"."deleted_at" is null') {
            $array = debug_backtrace();
//        foreach ($array as $row) {
//
//            var_dump($row['file'] . ':' . $row['line'] . '行,调用方法:' . $row['function']);
//        }
        }


        return $this->grammar->compileSelect($this);
    }
//    public function get($columns = ['*'])
//    {
//        dump($this->runSelect());
//        return collect($this->onceWithColumns(Arr::wrap($columns), function () {
//            return $this->processor->processSelect($this, $this->runSelect());
//        }));
//    }
}
